<?php declare(strict_types=1);

namespace App\Bac\Controller\RBAC;

use App\Abstract\Controller;
use App\Bac\Handler\RBAC\PermissionHandler;
use App\Bac\Request\RBAC\PermissionRequest;
use App\Helper\RespHelper;
use Hyperf\Di\Annotation\Inject;

class PermissionController extends Controller
{
    #[Inject]
    protected PermissionHandler $handler;

    //权限列表
    public function index(): array
    {
        return RespHelper::success($this->handler->getList());
    }

    public function create()
    {
        $request = \Hyperf\Support\make(PermissionRequest::class);
        $request->scene('add')->validateResolved();
        $data = $request->all();
        if (!$this->handler->addPermission($data))
            return RespHelper::fail();
        return RespHelper::success();
    }

    public function edit()
    {
        $request = \Hyperf\Support\make(PermissionRequest::class);
        $request->scene('edit')->validateResolved();
        $data = $request->all();
        $id = $data['id'];
        unset($data['id']);
        if (!$this->handler->editPermission((int)$id, $data))
            return RespHelper::fail();
        return RespHelper::success();
    }

    public function del()
    {
        $ids = $this->request->input('ids');
        if (!$this->handler->delPermission((int)$ids))
            return RespHelper::fail();
        return RespHelper::success();
    }

    public function treeList()
    {
        $data = $this->handler->treeList();
        return RespHelper::success($data);
    }

}
